import { ref } from 'vue';
import { useRouter } from 'vue-router';

export function useChildIndexRoutes() {
    const routesInfo = ref([]);
    const router = useRouter();
    // 找到index路由
    const indexRoute = router.getRoutes().find(r => r.name === 'index');
    // 获取子路由数组
    const childRoutes = indexRoute.children;
    // 提取子路由的path和meta信息
    routesInfo.value = childRoutes
        .map(child => {
            // 如果child.component为undefined, 并且有子路由, 则使用第一个子路由替换
            if (child.component === undefined && child.children && child.children.length > 0) {
                return {
                    path: child.children[0].path,
                    meta: child.meta
                };
            } else {
                return {
                    path: child.path,
                    meta: child.meta
                };
            }
        })
    return routesInfo;
}